<?php
/**
 * ChangePasswordForm.php
 *
 * PHP version 5
 *
 * LICENSE: This source file is subject to version 2.0 of the Apache license
 * that is available through the world-wide-web at the following URI:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * @category Controller
 * @package  Controller
 * @author   Alex Kopp <koppa@rpi.edu>
 * @license  http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
 * @link     http://http://code.google.com/p/tri-chi/
 */

/**
 * ChangePasswordForm
 *
 * The model that validates the change password form
 *
 * @category Controller
 * @package  Controller
 * @author   Alex Kopp <koppa@rpi.edu>
 * @license  http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
 * @link     http://http://code.google.com/p/tri-chi/
 */
class ChangePasswordForm extends CFormModel
{
    public $currentPassword;
    public $newPassword;
    public $newPassword2;

    /**
     * Declares the validation rules.
     * 
     * @return array
     */
    public function rules()
    {
        // Current Password, New Password, and  New Password (2) are all required
        // New Password MUST be the same as New Password (2)
        // Current Password MUST be correct
        return array(
            array('currentPassword, newPassword, newPassword2','required'),
            array('newPassword', 'compare', 'compareAttribute'=>'newPassword2'),
            array('currentPassword', 'correctPassword')
        );
    }

    /**
     * Validates Correct Password
     * 
     * @param unknown_type $attribute attribute we are validating
     * @param unknown_type $params    list of parameters
     * 
     * @return void
     */
    public function correctPassword($attribute,$params)
    {
        $model=Users::model()->findByPk(Yii::app()->user->name);
        if (sha1($this->$attribute) != $model->password) {
            $this->addError($attribute, 'Invalid Password');
        }
    }
    /**
     * Declares customized attribute labels.
     * If not declared here, an attribute would have a label that is
     * the same as its name with the first letter in upper case.
     * 
     * @return array
     */
    public function attributeLabels()
    {
        return array(
            'currentPassword'=>'Current Password',
            'newPassword'=>'New Password',
            'newPassword2'=>'New Password (Again)'
        );
    }
}